<template>
  <div>
    <el-dialog :title="noticeItemObj.title" :visible.sync="dialogVisible" width="70%" >
      <span v-html="noticeItemObj.content"></span>
      <span slot="footer" class="dialog-footer ">
        <el-button type="primary" @click="toRead">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getNoticeInfo ,} from "@/api/notice"
import {setUserNoticeRead} from "@/api/index";
import { getlocalStorage,setlocalStorage,removelocalStorage } from '@/utils/auth'
export default {
  name: "index",
  data() {
    return {
      dialogVisible: false,
      noticeItemObj: {},
      timer: null
    }
  },
  props: {
    noticeList: {
      require: true,
      type: Array,
    },

    noticetype: {
      type: String,
    }
  },
  methods: {
    getContent(){
      this.noticeList.forEach((item) =>{
        getNoticeInfo(item.noticeId)
          .then((response) => {
            this.noticeItemObj = response.data;
            for (var itemKey in item) {
              this.noticeItemObj[itemKey]  = item[itemKey]
            }
            this.dialogVisible  = true;
            console.log( this.noticeItemObj )
          })
          .catch((err) => {
            this.$message.error(err.message || "error");
          });
      })
    },
    getTime(){
      var readTime =  this.$store.state.user.readTime;
      var that = this;
      if (readTime) {
        that.timer = setInterval(() => {
          readTime--
          this.$store.commit('user/SET_READTIME',readTime);
          if (readTime <= 0) {
            clearInterval( that.timer)
            removelocalStorage('toNoAnonymousRead');
            removelocalStorage('toAnonymousRead');
            removelocalStorage('readTime');
          }
        }, 1000)
      }
    },
    toRead(){
      if (!this.noticeItemObj.isAnonymous) {
        var data = {
          "noticeId": this.noticeItemObj.noticeId,
          "noticePositionId": this.noticeItemObj.noticePositionId
        }
        setUserNoticeRead(data).then((response) =>{
          setlocalStorage('toNoAnonymousRead',true);
          this.dialogVisible = false;
          this.$store.commit('user/SET_READTIME',60);
          this.getTime();
        }).catch((err) =>{
          this.$message.error(err.message)
        });
      }else {
        setlocalStorage('toAnonymousRead',true);
        this.dialogVisible = false;
        this.$store.commit('user/SET_READTIME',60);
        this.getTime();
      }
    }
  },
  mounted(){
   this.getContent();
  }
}
</script>

<style scoped lang="scss"></style>
